﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FUI;
using System.IO;
using System.Text.RegularExpressions;
using CUILoader;
using FUINetCF;
using System.Xml;

namespace MainMenuFUI
{
    class CMainMenuFUI :FUI.FUIBase
    {
        public CMainMenuFUI()
        {
            StrName = "NetCFMainMenu";
            loadXMLProperties();
        }

      
        public override string generateDeclaration()
        {
            FileStream file;
            file = new FileStream(StrPath + "\\NetCFMainMenuDeclaration.txt", FileMode.OpenOrCreate, FileAccess.Read);
            StreamReader sr = new StreamReader(file);
            string s = sr.ReadToEnd();
            sr.Close();
            file.Close();
            string pattern = "###MainMenuName###";
            string replacement = generateDataType(this.getAttributeFromName("Name"));
            Regex rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);
            return s;
        }

        public override string generateInitialization()
        {
            FileStream file;
            file = new FileStream(StrPath + "\\NetCFMainMenuInitialization.txt", FileMode.OpenOrCreate, FileAccess.Read);
            StreamReader sr = new StreamReader(file);
            string s = sr.ReadToEnd();
            sr.Close();
            file.Close();

            //Su dung regex de thay cac pattern trong Initialization.txt

            //Add them các Items
            CAttribute attItemCollection = new CAttribute();
            foreach (CAttribute att in LstAttributes)
            {
                if (att.StrName == "ItemCollection")
                {
                    attItemCollection = att;
                    break;
                }
            }
            if (attItemCollection.StrValue != "")
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(attItemCollection.StrValue);

                XmlNodeList nodes = xmlDoc.GetElementsByTagName("Item");
                foreach (XmlNode n in nodes)
                {
                    //this.menuItem1 = new System.Windows.Forms.MenuItem();
                    s += "\n\tprivate System.Windows.Forms.MenuItem " + n.Attributes["ItemName"].Value + ";";
                    s += "\n\tthis." + n.Attributes["ItemName"].Value + " = new System.Windows.Forms.MenuItem();";
                    s += "\n\tthis." + n.Attributes["ItemName"].Value + ".Text = \"" + n.Attributes["ItemText"].Value + "\";";
                    s += "\n\tthis.###MainMenuName###.MenuItems.Add(this." + n.Attributes["ItemName"].Value + ");";
                }
            }

            string pattern = "###MainMenuName###";
            string replacement = generateDataType(this.getAttributeFromName("Name"));
            Regex rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);          

            return s;
        }

        public override string generateDataType(CAttribute m_Att)
        {
            CNetCFConverter m_NetCFConverter = new CNetCFConverter();
            return m_NetCFConverter.NetCFGenerateDataType(m_Att);
        }
    }
}
